package Classes.GameDatas// ActionScript file
{
	import Classes.GameUtils.GU_DataTransform;
	import Classes.Managers.M_ConfigData;
	import Classes.staticinfo.StaticInfo;
	
	import UIComponents.IGD_ShopItem;

	/**
	 *商店数据类 
	 * @author upstream
	 * 
	 */	
	public class GD_Shop extends GameData
	{
		private static var _instance:GD_Shop;
		private var _shopItems:Object;
		public function GD_Shop():void
		{
			this.init();
		}
		
		private function init():void
		{
			this._shopItems=new Object;
			var shopConfigObjsCSV:String=M_ConfigData.instance.getCSVByName("商店配置表");
			var shopConfigObjsAry:Array=GU_DataTransform.csvTransformObjsAry(shopConfigObjsCSV,11);
			for each(var shopConfigDataObj:Object in shopConfigObjsAry)
			{
				//过滤掉已经下架的物品
				if(shopConfigDataObj.zt_status!=0)
				{
					//物品类型
					var itemType:Number=Number(shopConfigDataObj.zt_item_type);
					/**
					 *   物品 --类型为：101,102,103  ；收藏品 --类型为：130 
					 * 	  武器 --类型为：110,111,112,113,114
					 * 	  药品 --类型为：120,121,123,124   暂时不用
					 *   建筑---401；装饰--402
					 * 	 金币兑换--类型为:100
					 */	
					var shopItem:GD_ShopItem;
					switch(itemType)
					{
						case 101:
						case 102:
						case 103:
						case 104:
						case 130:
							var gd_item:GD_Item=new GD_Item(shopConfigDataObj.zt_item_id,0);
							shopItem=GD_ShopItem.creat(shopConfigDataObj,gd_item);
							this._shopItems[shopItem.shopItemType]=shopItem;
							break;
						case 110:
						case 111:
						case 112:
						case 113:
						case 114:	
							var gd_weapon:GD_Weapon=new GD_Weapon(shopConfigDataObj.zt_item_id,0);
							shopItem=GD_ShopItem.creat(shopConfigDataObj,gd_weapon);
							this._shopItems[shopItem.shopItemType]=shopItem;
							break;
						case 401:
						case 402:
							var gd_build:GD_BuildingItem=new GD_BuildingItem(shopConfigDataObj.zt_item_id);
							shopItem=GD_ShopItem.creat(shopConfigDataObj,gd_build);
							this._shopItems[shopItem.shopItemType]=shopItem;
							break;
						case 100:												
							shopItem=GD_ShopItem.creat(shopConfigDataObj,null);
							this._shopItems[shopItem.shopItemType]=shopItem;
							break;
					}
				}
			}
		}
		
		/**
		 * 根据物品ID跟货币类型来获取商品信息 
		 * @param itemId  物品ID
		 * @param moneyType 货币类型   0 表示欧币 1 表示金币 2表示物品
		 * @return 
		 * 
		 */		
		public function getShopInfoByItemInfo(itemId:Number,moneyType:Number=0):IGD_ShopItem
		{
			var tempShopItem:IGD_ShopItem;
			for each(var shopItem:IGD_ShopItem in this.shopItems)
			{
				if(shopItem.shopItemId==itemId && shopItem.moneyType==moneyType)
				{
					tempShopItem=shopItem;
					break;
				}
			}
			return tempShopItem;
		}
		
		public function deleteShopItem(shopId:Number):void
		{
			delete this.shopItems[shopId];
			//发出商店物品过期的通知，接受者刷新数据，弹出友情提示
			this.touchObserve(StaticInfo.DELETE_SHOP_ITEM);
		}
		
		
		/**
		 *根据物品类型返回商店道具数据 
		 * @param secondType 商品第二类型值
		 * @return 
		 * 
		 */		
		public function getShopItemsAryByItemType(itemType:Number):Array
		{
			var returnAry:Array=new Array;
			for each(var shopItem:GD_ShopItem in this.shopItems)
			{
				if(shopItem.secondType==itemType)
				{
					returnAry.push(shopItem);
				}
			}
			returnAry.sortOn("shopItemType",Array.NUMERIC);
			return returnAry;
		}

		/*getter & setter*/
		/**
		 * 获得装饰数组从商店 
		 * @return 
		 * 
		 */		
		public function get decorationItemsAry():Array
		{
			return this.getShopItemsAryByItemType(402);
		}
		
		/**
		 * 获得建筑数组从商店 
		 * @return 
		 * 
		 */		
		public function get buildingItemsAry():Array
		{
			return this.getShopItemsAryByItemType(401);
		}
		
		public static function get instance():GD_Shop
		{
			if(!_instance)_instance=new GD_Shop();
			return _instance;
		}

		public function get shopItems():Object
		{
			return _shopItems;
		}


	}
}